package com.gzz.orchid.common.webmvc;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.MessageSource;
import org.springframework.core.env.Environment;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Set;

/**
 * @author higmd
 * 特点
 * 功能强大：通过 ApplicationArguments 提供了对命令行参数的灵活解析，包括获取选项和非选项参数。
 * 适合场景：适用于需要对命令行参数进行复杂解析的场景。
 */
@Slf4j
public class SpringApplicationRunner implements ApplicationRunner {
    @Autowired
    private Environment environment;
    @Autowired
    private MessageSource messageSource;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = environment.getProperty("server.port");
        String path = environment.getProperty("server.servlet.context-path");

        //log.info("Spring Boot 完成启动之后执行 ApplicationRunner, 加载字典数据到缓存中。。。");
        // log.info("order值： 2");
        log.info("原始参数：" + Arrays.asList(args.getSourceArgs()));
        Set<String> optionNames = args.getOptionNames();
        for (String key : optionNames) {
            log.info("解析后的key: [" + key + "]  value: " + args.getOptionValues(key));
        }
        log.info("非选项参数： " + args.getNonOptionArgs());
        log.info("=======================================================\n\t" +
                "Application Admin api is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + path + "/\n\t" +
                "External: \thttp://" + ip + ":" + port + path + "/\n\t" +
                //"Swagger-UI: \t\thttp://" + ip + ":" + port + path + "/doc.html\n" +
                "----------------------------------------------------------");
        try {
            log.info("\n\t****************** JVM 信息 **********************");
            String[] properties =
                    {"java.home", "java.version", "java.vm.name", "java.vm.vendor", "os.name", "user.dir"};
            for (String property : properties) {
               System.out.println(String.format("* %s = %s",  property, System.getProperty(property)));
            }
            MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
            MemoryUsage heapUsage = bean.getHeapMemoryUsage();
            log.info("\n\t******************JVM内存参数********************** \n\t"
                    + "* 初始内存 = " + heapUsage.getInit() / 1024 / 1024 + "M \n\t"
                    + "* 已使用内存 = " + heapUsage.getUsed() / 1024 / 1024 + "M \n\t"
                    + "* 已提交内存 = " + heapUsage.getCommitted() / 1024 / 1024 + "M \n\t"
                    + "* 最大内存 = " + heapUsage.getMax() / 1024 / 1024 + "M \n\t");
        } catch (Exception e) {
            log.error("异常: {}", e.getMessage());
        }
    }


}